
import type { AxiosRequestConfig, AxiosProgressEvent } from 'axios';
import type { UploadProgressEvent, UploadRequestOptions} from 'element-plus'
import axios from 'axios';

// 需要以二进制上传不是formData，所以需要自定义上传方法
// function customUpload(options: UploadRequestOptions) {
//     console.log('custom upload:', options)
//     const config:AxiosRequestConfig = {
//         headers: {
//             'Content-Type': 'application/octet-stream',
//             'Authorization': undefined
//         },
//         onUploadProgress: (progressEvent: AxiosProgressEvent) => {
//             if (progressEvent.lengthComputable) {
//                 const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total!);
//                 const v:UploadProgressEvent = {
//                     percent: percentCompleted
//                 } as UploadProgressEvent  // 如果不进行类型断言，会提示只初始化了部分属性

//                 options.onProgress(v)
//             }
//         },
//     };
//     axios.put(upload_url.value, options.file, config)
//     .then((res) => {
//         options.onSuccess(res.data);
//         console.log('upload success:')
//     })
//     .catch((err) => {
//         console.error('Error uploading file:', err);
//         options.onError(err);
//     })
// }

export function minio_upload(url: string, file: File, onProgress: (progress: UploadProgressEvent) => void) {
    const config:AxiosRequestConfig = {
        headers: {
            'Content-Type': 'application/octet-stream',
            'Authorization': undefined
        },
        onUploadProgress: (progressEvent: AxiosProgressEvent) => {
            if (progressEvent.lengthComputable) {
                const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total!);
                const v:UploadProgressEvent = {
                    percent: percentCompleted
                } as UploadProgressEvent  // 如果不进行类型断言，会提示只初始化了部分属性

                onProgress(v)
            }
        },
    }
    return axios.put(url, file, config)
}